/*
 * @Author: your name
 * @Date: 2021-02-16 12:40:22
 * @LastEditTime: 2021-02-18 23:47:03
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \demo_2021021601\driver\at32F4xx\bsp_delay.c
 */

#include "bsp.h"

/*delay variable*/
static __IO float fac_us;
static __IO float fac_ms;
/*delay macros*/
#define STEP_DELAY_MS 500

#define DELAY_MAX 0xFFFFFFFFU
/**
  * @brief  initialize Delay function   
  * @param  None
  * @retval None
  */
void bsp_InitDelay(void)
{
#if 0
    /*Config Systick*/
    SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
    fac_us = (float)SystemCoreClock / (8 * 1000000);
    fac_us = 15;
    fac_ms = 15000;
#endif
#if 1
    uint32_t Ticks = 1000;
    /* Configure the SysTick to have interrupt in 1ms time base */
    SysTick->LOAD = (uint32_t)((120000000 / Ticks) - 1UL); /* set reload register */
    SysTick->VAL = 0UL;                                    /* Load the SysTick Counter Value */
    SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk |
                    SysTick_CTRL_TICKINT_Msk |
                    SysTick_CTRL_ENABLE_Msk; /* Enable the Systick Timer */

    //NVIC_SetPriority(SysTick_IRQn, 0xFF);
    NVIC_EnableIRQ(SysTick_IRQn);
#endif
}

/**
  * @brief  Inserts a delay time.
  * @param  usNus: specifies the delay time length, in microsecond.
  * @retval None
  */
void bsp_DelayUS(uint32_t usNus)
{
    uint32_t ulTemp;

    SysTick->LOAD = (uint32_t)(usNus * fac_us);
    SysTick->VAL = 0x00;
    SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
    do
    {
        ulTemp = SysTick->CTRL;
    } while ((ulTemp & 0x01) && !(ulTemp & (1 << 16)));

    SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
    SysTick->VAL = 0X00;
}

/**
  * @brief  Inserts a delay time.
  * @param  usNms: specifies the delay time length, in milliseconds.
  * @retval None
  */
void bsp_DelayMS(uint16_t usNms)
{
#if 0
    uint32_t ulTemp;

    while (usNms)
    {
        if (usNms > STEP_DELAY_MS)
        {
            SysTick->LOAD = (uint32_t)(STEP_DELAY_MS * fac_ms);
            usNms -= STEP_DELAY_MS;
        }
        else
        {
            SysTick->LOAD = (uint32_t)(usNms * fac_ms);
            usNms = 0;
        }
        SysTick->VAL = 0x00;
        SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
        do
        {
            ulTemp = SysTick->CTRL;
        } while ((ulTemp & 0x01) && !(ulTemp & (1 << 16)));

        SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
        SysTick->VAL = 0X00;
    }
#endif
    __IO uint32_t tmp = SysTick->CTRL; /* Clear the COUNTFLAG first */
    uint32_t tmpDelay;                 /* MISRAC2012-Rule-17.8 */
    /* Add this code to indicate that local variable is not used */
    ((void)tmp);
    tmpDelay = usNms;
    /* Add a period to guaranty minimum wait */
    if (tmpDelay < DELAY_MAX)
    {
        tmpDelay++;
    }

    while (tmpDelay != 0U)
    {
        if ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) != 0U)
        {
            tmpDelay--;
        }
    }
}

/********************************************* End of file **********************************************/
